#include "HufArc.h"

void addFiles (char * fileNames[])
{
	FILE ** arc = NULL;
	FILE * fileToRead = NULL;
	char sign[] = _SIGN;
	char answer [256];
	char yes[] = "yes";
	char a[] = "XXXXXX";
	char *pos;
	char * arcnew = NULL;
	int i = 1;
	int rc = 0;
	int m = 0;
	struct stat * fileStat = (struct stat * ) calloc (1,sizeof(struct stat));
	fileHead * fileHd = NULL;
	fileHead ** list = NULL;
	///////////////////////
	if (!fileStat)
		cl_exit (ADD_FILES,MEMORY_ERR,arc,arcnew,fileHd,fileToRead);
	arcnew = (char * ) calloc (strlen(fileNames[0]) + 7,sizeof(char));
	if (!arcnew)
		cl_exit (ADD_FILES,MEMORY_ERR,arc,arcnew,fileHd,fileToRead);
	strcat (arcnew,fileNames[0]);
	strcat (arcnew,a);
	arc = openArc (fileNames[0],arcnew);
	
	if (arc[0])
	{
		if ((rc = testing (arc[0])) == 1)
			cl_exit (ADD_FILES,CS_ERR,arc,arcnew,fileHd,fileToRead);
	}
	
	if (!arc[0])
	{
		if (arc[1])
		{
			fwrite (sign, sizeof(char), 7, arc[1]);
			while(fileNames[i])
			{
				fileHd = makeFileHead(fileNames[i]);
				fileToRead = fopen (fileNames[i],"rb");
				if (fileToRead)
				{
					if (rc != 0) 
						cl_exit (ADD_FILES,WRITE_ERR,arc,arcnew,fileHd,fileToRead);
					free(fileHd->fileName);
					free(fileHd);
					fclose (fileToRead);
				} else {
					cl_exit (ADD_FILES, OPEN_FILE_ERR,arc,arcnew,fileHd,fileToRead);//!!!!!!!!!!!!!!
					return;
				}
				i++;
			}
			fclose (arc[1]);
			clean_files (fileNames[0],arcnew,NEW_ARC_2);
			free (arcnew);
			free (arc);
			return;
		}

	} else {
		list = listingFile (arc[0]);
		while (fileNames[i])
		{
			pos = getName (fileNames[i]);
			stat (fileNames[i],fileStat);
			m = 0;
			while (list[m])
			{
				if (!(strcmp (pos,list[m]->fileName)))
				{
					printf ( "Do you want replace:\n%s\nSize: %.3f Kb\nDate: %s\n"
							"To:\n%s\nSize: %.3f Kb\nDate: %s"
							"\n",pos,((float)list[m]->fileSize)/1024.0,ctime((time_t *)&list[m]->mtime),pos,fileStat->st_size / 1024.0,ctime(&fileStat->st_mtime));
					printf ("Input Yes or no: ");
				scanf ("%s",answer);
					if (!(strnicmp(answer,yes,3)))
					{
						list[m]->rplFlag = REPL_FILE;
					} else {
						list[m]->rplFlag = 0;
						fileNames[i][0] = 0;
					}
				}
				m++;
			}
			i++;
		}
		copyArc (arc,list);
		i = 1;
		m = 0;
		while (list[m])
		{
			free(list[m]->fileName);
			free(list[m]);
			m++;
		}
		free(list);
		while (fileNames[i])
		{
			if (strlen(fileNames[i]) != 0)
			{
					fileHd = makeFileHead(fileNames[i]);
					fileToRead = fopen (fileNames[i],"rb");
					if (fileToRead)
					{
						rc = writeToArc (arc[1],fileToRead,fileHd);
						if (rc != 0) 
							cl_exit (ADD_FILES, WRITE_ERR,arc,arcnew,fileHd,fileToRead);
						free(fileHd->fileName);
						free(fileHd);
						fclose (fileToRead);
					} else {
						cl_exit (ADD_FILES, OPEN_FILE_ERR,arc,arcnew,fileHd,fileToRead);//!!!!!!!
						return;
					}
			////////////////////////////////////work with including arc
			}
			i++;
		}
		free(fileStat);
		fclose (arc[0]);
		fclose (arc[1]);
		free (arc);
		rc = clean_files ( fileNames[0],arcnew,NEW_ARC);
		free (arcnew);
	//	printf ("END\n");
	}
}